/**
 * 试题异步
 */

function question_view_ajax(){
    var _this = this;
    _this.body_id;
    _this.question_url = appPath + '/popup/getQuestionByIds'; //根据questionIDs 获取试题
    _this.questionIds;
    _this.type;
    _this.question_paper = 0;   //questionIds 默认加载第一组
    _this.questionLength;
    _this.load_length;


    _this.initMethod = function(body_id, course_id,type, load_length){
        _this.body_id = body_id;
        _this.load_length = load_length;
        _this.intTable(course_id,type);

        //点击加载更多
        $("#loadQuestion").click(function () {
            _this.addQuestion();
            $("#question_body").find('.subject_foot').show();
        });
        $("#question_body").find('.subject_foot').toggle();
    };

    _this.intTable = function (course_id,type) {
        var url = appPath + "/popup/course_question_view_ajax/number";
        var param = {"course_id" : course_id,"type":type};
        var data = _this.ajax(url, param);
        var code = data['code'];
        if(code == '10000'){
            _this.questionIds = data['result']['questions'];
            _this.questionLength = data['result']['length'];
            _this.type = data['result']['type'];
            if(_this.questionLength == 0){
                $("#loadQuestion").hide();
                //没题
                $("#" + _this.body_id).find('[id^=div]').end().append('<div><div class="no-content"></div></div>');
                return
            }
            _this.addQuestion();
        }
    };


    _this.addQuestion = function () {
        if(_this.question_paper > _this.questionIds.length - 1){
            $("#loadQuestion").hide();
            return;
        }
        var params = {"questionIds": _this.questionIds[_this.question_paper], "projectId" : _this.project_id,"type": _this.type};
        var data = _this.ajax(_this.question_url, params);
        var code = data['code'];
        if(code == '10000'){
            var questions = data['result'];
            var html = '';
            if(questions.length < 1 && _this.question_paper == 0){
                $("#loadQuestion").hide();
                html = '<div><div class="no-content"></div></div>';
            }
            var checkStatus = $('#toggle').is(":checked");
            var question_div_num = $("#" + _this.body_id).find('[id^=div]').length;
            var card_count = 0;
            for(var i = 0; i < questions.length; i++){
                var question = questions[i];
                var chrType = question.chrType;
                if(chrType == '01' || chrType == '02'){
                    // html += _this.single_many_html(question, _this.question_paper * _this.load_length + i, checkStatus);
                    html += _this.single_many_html(question, question_div_num + i, checkStatus);
                    card_count += 1;
                }else if(chrType == '03'){
                    // html += _this.judge_html(question, _this.question_paper * _this.load_length + i, checkStatus);
                    html += _this.judge_html(question, question_div_num + i, checkStatus);
                    card_count += 1;

                    // 填空题
                } else if(chrType == '04'){
                    html += _this.fill_html(question, question_div_num + i, checkStatus);
                    card_count += 1;

                    // 简答 论述 计算  绘图
                } else if(chrType == '05' || chrType == '06' || chrType == '07' || chrType == '08') {
                    html += _this.subjective_html(question, question_div_num + i, checkStatus);
                    card_count += 1;
                }
            }

            var start_height = $("#card_list").height();

            $("#" + _this.body_id).find('[id^=div]').end().append(html);
            _this.initCard(card_count);
            var last_number = _this.questionLength - questions.length -  _this.question_paper * _this.load_length;
            if(last_number > 0){
                $("#loadQuestion").find("span").html(last_number);
            }else{
                $("#loadQuestion").hide();
            }

            var scroll_height = $("#card_list").find("li:eq(0)").height() * (
                $("#card_list").find("li").length / 4 == 0 ?
                    $("#card_list").find("li").length / 4 :
                    $("#card_list").find("li").length / 4 + 1);

            $("#card_list").scrollTop(scroll_height + $("#card_list").scrollTop());
            _this.question_paper++;
            //判断是否加载完
            if(_this.question_paper > _this.questionIds.length - 1){
                $("#loadQuestion").hide();
                return;
            }
        }
        //图片懒加载
        $("img.lazy").lazyload({
            effect: "fadeIn",
            threshold : 20000
        });
    };

    _this.initCard = function (len) {
        var aLi = '';
        var div_num = $('#card_list li').length;
        for( var i = 0; i < len; i++ ){
            // aLi += '<li>' + (i + 1 + _this.question_paper * _this.load_length) + '</li>';
            aLi += '<li>' + (i + 1 + div_num) + '</li>';
        }
        //加载答题卡
        $('#card_list').append(aLi);
        //答题卡部分模拟滚动条
        $('#card_list').niceScroll();
    };

    _this.single_many_html = function (question, i, checkStatus) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions" data-course="' + _this.course_id + '" data-question="' + question.intId + '" data-project="' + _this.project_id + '" data-index="' + i + '"';
        if(checkStatus){
            html += ' style="display: none;">';
        }else {
            html += '>';
        }
        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（';
        if(chrType == '02'){
            html += '多选题';
        }else{
            html += '单选题';
        }
        html += '）</span><div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt=""  src="' + file.mimeUrl + '">';
                }
            }
        }
        html += '</div>';
        html += '</div>';
        html += '<div class="options">';
        var options = question.options;
        if(options != undefined){
            for(var i=0; i<options.length; i++){
                var q = options[i];
                html += '<div>';
                if(chrType == '02'){
                    html += '<div class="option checkbox" data-type="' + q.oType + '">';
                }else{
                    html += '<div class="option" data-type="' + q.oType + '">';
                }
                html += '<span></span>';
                html += '<div><span>' + q.oType + '、</span><div>' + q.optionContent + '</div></div>';
                html += '</div>';
                html += '<div class="show_img clearfix">';
                if(q.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + q.mimeUrl + '" alt=""  src="' + q.mimeUrl + '">';
                }
                html += '</div>';
                html += '</div>';
            }
        }
        html += '</div>';
        /*html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        html += '<button type="button" class="btn btn-info sure">确定</button>&nbsp;&nbsp;';
        html += '<button type="button" class="btn btn-mark">标记</button>';
        html += '</div>';
        html += '<div class="pull-right">';
        html += '<a href="javascript:;" class="remove_error" style="display: none;"><i class="fa fa-remove"></i> 移除错题</a>&nbsp;&nbsp;';
        html += '<a href="javascript:feed_back(\''+question.intId+'\');" class="wrong"><i class="fa fa-times"></i> <span>错题反馈</span></a>&nbsp;&nbsp;';
        var chrIsCollect = question.chrIsCollect;
        if(chrIsCollect == true || chrIsCollect == 'true'){
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star on"></i> <span>取消收藏</span></a>&nbsp;&nbsp;';
        }else{
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star-o" ></i> <span>收藏试题</span></a>&nbsp;&nbsp;';
        }
        // html += '<a href="javascript:;" class="analy" style="display: block;">展开解析 <i class="fa fa-angle-down"></i></a>';
        html += '</div>';
        html += '</div>';*/
        var analysisContent = question.analysisContent;
        // if(analysisContent != '' && analysisContent != undefined){
            html += '<div class="subject_foot" style="border-top:0px;">';
            html += '<div><span class="label">正确答案：</span><i class="answer">' + aes_decrypt(question.varAnswer) + '</i></div>';
            html += '<div class="clearfix">';
            html += '<span class="label">试题解析：</span>';
            html += '<div class="pull-left analysis">';

            if(analysisContent == '' || analysisContent == undefined){
                html += '暂无解析';
            }else{
                html += question.analysisContent;
                html += '<div class="show_img clearfix">';
                var analysisFiles = question.analysisFiles;
                if(analysisFiles != undefined && analysisFiles.length > 0){
                    for(var i=0; i<analysisFiles.length; i++){
                        var file = analysisFiles[i];
                        if(file.mimeType == 'img'){
                            html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt=""  src="' + file.mimeUrl + '">';
                        }
                    }
                }
                html += '</div>';
            }
            html += '</div>';
            html += '</div>';
            html += '</div>';
        // }
        html += '</div>';
        console.log(html)
        return html;
    };

    _this.judge_html = function (question, i, checkStatus) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions" data-course="' + _this.course_id + '" data-question="' + question.intId + '" data-project="' + _this.project_id + '" data-index="' + i + '"';
        if(checkStatus){
            html += ' style="display: none;">';
        }else {
            html += '>';
        }
        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（判断题）</span>' + '<div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt=""  src="' + file.mimeUrl + '">';
                }
            }
        }
        html += '</div>';
        html += '</div>';
        html += '<div class="options">';
        html += '<div>';
        html += '<div class="option" data-type="A">';
        html += '<span></span>';
        html += '<div><span>A、</span><div>正确</div></div>';
        html += '</div>';
        html += '</div>';
        html += '<div>';
        html += '<div class="option" data-type="B">';
        html += '<span></span>';
        html += '<div><span>B、</span><div>错误</div></div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';

        /*html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        html += '<button type="button" class="btn btn-info sure">确定</button>&nbsp;&nbsp;';
        html += '<button type="button" class="btn btn-mark">标记</button>';
        html += '</div>';
        html += '<div class="pull-right">';
        html += '<a href="javascript:;" class="remove_error" style="display: none;"><i class="fa fa-remove"></i> 移除错题</a>&nbsp;&nbsp;';
        html += '<a href="javascript:feed_back(\''+question.intId+'\');" class="wrong"><i class="fa fa-times"></i> <span>错题反馈</span></a>&nbsp;&nbsp;';
        var chrIsCollect = question.chrIsCollect;
        if(chrIsCollect == true || chrIsCollect == 'true'){
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star on"></i> <span>取消收藏</span></a>&nbsp;&nbsp;';
        }else{
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star-o" ></i> <span>收藏试题</span></a>&nbsp;&nbsp;';
        }
        // html += '<a href="javascript:;" class="analy" style="display: block;">展开解析 <i class="fa fa-angle-down"></i></a>';
        html += '</div>';
        html += '</div>';*/
        var analysisContent = question.analysisContent;
        // if(analysisContent != '' && analysisContent != undefined){
            html += '<div class="subject_foot" style="border-top:0px;">';
            html += '<div><span class="label">正确答案：</span><i class="answer">' + aes_decrypt(question.varAnswer) + '</i></div>';
            html += '<div class="clearfix">';
            html += '<span class="label">试题解析：</span>';
            html += '<div class="pull-left analysis">';

            if(analysisContent == '' || analysisContent == undefined){
                html += '暂无解析';
            }else{
                html += question.analysisContent;
                html += '<div class="show_img clearfix">';
                var analysisFiles = question.analysisFiles;
                if(analysisFiles != undefined && analysisFiles.length > 0){
                    for(var i=0; i<analysisFiles.length; i++){
                        var file = analysisFiles[i];
                        if(file.mimeType == 'img'){
                            html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt=""  src="' + file.mimeUrl + '">';
                        }
                    }
                }
                html += '</div>';
            }
            html += '</div>';
            html += '</div>';
            html += '</div>';
        // }
        html += '</div>';
        return html;
    };

    // 填空题
    _this.fill_html = function (question, i, checkStatus) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions" data-course="' + _this.course_id + '" data-question="' + question.intId + '" data-project="' + _this.project_id + '" data-index="' + i + '"';
        if(checkStatus){
            html += ' style="display: none;">';
        }else {
            html += '>';
        }
        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（填空题）</span>' + '<div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="'+ file.mimeUrl + '">';
                }
            }
        }
        html += '</div>';
        html += '</div>';


        /*html += '<div class="options">';

        var titNum = checkTianKongNum(question.varQuestionsContent);
        for (var j = 0; j < titNum; j++) {

            html += '<div>';
            html += '<div>';
            html += '<span></span>';
            html += '<div><span>空 '+ (j+1) +' 答案、</span><div><input type="text" width="60px"/></div></div>';
            html += '</div>';
            html += '</div>';

        }

        html += '</div>';*/


        /*html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        html += '<button type="button" class="btn btn-info subjective">确定</button>&nbsp;&nbsp;';
        html += '<button type="button" class="btn btn-mark">标记</button>';
        html += '</div>';
        html += '<div class="pull-right">';
        html += '<a href="javascript:;" class="remove_error" style="display: none;"><i class="fa fa-remove"></i> 移除错题</a>&nbsp;&nbsp;';
        html += '<a href="javascript:feed_back(\''+question.intId+'\');" class="wrong"><i class="fa fa-times"></i> <span>错题反馈</span></a>&nbsp;&nbsp;';
        var chrIsCollect = question.chrIsCollect;
        if(chrIsCollect == true || chrIsCollect == 'true'){
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star on"></i> <span>取消收藏</span></a>&nbsp;&nbsp;';
        }else{
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star-o" ></i> <span>收藏试题</span></a>&nbsp;&nbsp;';
        }
        html += '<a href="javascript:;" class="analy" style="display: none;">展开解析 <i class="fa fa-angle-down"></i></a>';
        html += '</div>';
        html += '</div>';*/


        html += '<div class="subject_foot" style="border-top:0px;">';

        html += '<div><span class="label">正确答案：</span><i class="answer">' + question.varAnswer + '</i></div>';

        // ***************************begin
        html += '<div class="show_img clearfix">';
        var varAnswerImages = question.varAnswerImages;
        if(varAnswerImages != undefined && varAnswerImages != '' && varAnswerImages.length > 0){
            var answerImagesArr = varAnswerImages.split(",");
            for (var j = 0; j <answerImagesArr.length ; j++) {
                html += '<img class="lazy" data-original="' + answerImagesArr[j] + '" alt="" src="'+ answerImagesArr[j] +'">';
            }
        }
        html += '</div>';

        // ***************************end


        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;
            if(analysisFiles != undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(file.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="' + file.mimeUrl + '">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;
    };

    // 其他主观题
    _this.subjective_html = function (question, i, checkStatus) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions" data-course="' + _this.course_id + '" data-question="' + question.intId + '" data-project="' + _this.project_id + '" data-index="' + i + '"';
        if(checkStatus){
            html += ' style="display: none;">';
        }else {
            html += '>';
        }
        html += '<div class="subject"><div class="question_content clearfix">';

        // 题型名称
        var titleTypeName = "";
        if (chrType == '05') {
            titleTypeName = "（简答题）";
        } else if (chrType == '06') {
            titleTypeName = "（论述题）";
        } else if (chrType == '07') {
            titleTypeName = "（计算题）";
        } else if (chrType == '08') {
            titleTypeName = "（绘图题）";
        }

        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">' + titleTypeName + '</span>' + '<div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="' + file.mimeUrl + '">';
                }
            }
        }
        html += '</div>';
        html += '</div>';


        /*html += '<div class="options">';

        html += '<div>';
        html += '<div>';
        html += '<span></span>';
        html += '<div><span>答案、</span><div><textarea style="width: auto"></textarea></div></div>';
        html += '</div>';
        html += '</div>';

        html += '</div>';*/

        /*html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        html += '<button type="button" class="btn btn-info subjective">确定</button>&nbsp;&nbsp;';
        html += '<button type="button" class="btn btn-mark">标记</button>';
        html += '</div>';
        html += '<div class="pull-right">';
        html += '<a href="javascript:;" class="remove_error" style="display: none;"><i class="fa fa-remove"></i> 移除错题</a>&nbsp;&nbsp;';
        html += '<a href="javascript:feed_back(\''+question.intId+'\');" class="wrong"><i class="fa fa-times"></i> <span>错题反馈</span></a>&nbsp;&nbsp;';
        var chrIsCollect = question.chrIsCollect;
        if(chrIsCollect == true || chrIsCollect == 'true'){
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star on"></i> <span>取消收藏</span></a>&nbsp;&nbsp;';
        }else{
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star-o" ></i> <span>收藏试题</span></a>&nbsp;&nbsp;';
        }
        html += '<a href="javascript:;" class="analy" style="display: none;">展开解析 <i class="fa fa-angle-down"></i></a>';
        html += '</div>';
        html += '</div>';*/


        html += '<div class="subject_foot" style="border-top:0px;">';

        html += '<div><span class="label">正确答案：</span><i class="answer">' + question.varAnswer + '</i></div>';

        // ***************************begin
        html += '<div class="show_img clearfix">';
        var varAnswerImages = question.varAnswerImages;
        if(varAnswerImages != undefined && varAnswerImages != '' && varAnswerImages.length > 0){
            var answerImagesArr = varAnswerImages.split(",");
            for (var j = 0; j <answerImagesArr.length ; j++) {
                html += '<img class="lazy" data-original="' + answerImagesArr[j] + '" alt="" src="'+ answerImagesArr[j] +'">';
            }
        }
        html += '</div>';

        // ***************************end


        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;
            if(analysisFiles != undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(file.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="'+ file.mimeUrl +'">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;

    }

    // 检查题目内容括号个数
    function checkTianKongNum(title) {

        // 正则表达式
        var reg = /[\(|（]([^\(\)（）]*)[\)|）]/ig;

        var t = title.match(reg);

        if (t == null) {
            return 0;
        }

        // 总共括号个数
        var num = title.match(reg).length;

        // 返回括号个数
        return num;

    }


    _this.ajax = function(url, param, type) {
        var result;
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : param,
            success : function(data) {
                result = data;
            }
        });
        return result;
    };
}

var question_view_ajax = new question_view_ajax();